# Eta-Square
# Will compare the similarity between 2 vectors
## Arguments
#### - vec.1 => 1st vector usually representing a connectivity map
#### - vec.2 => 2nd vector usually representing another connectivity map
## Returns
#### - eta = 0-1 where 1=identical or perfectly similar 2 vectors
etasq = function(vec.1, vec.2) {

    vec.total = c(vec.1, vec.2)

    vec.mean = (vec.1 + vec.2)/2
    grand.mean = mean(vec.mean)
    vec.mean = rep(vec.mean, times=2)
    
    ss.within = sum((vec.total - vec.mean)^2)
    ss.total = sum((vec.total - grand.mean)^2)
    
    eta = 1 - (ss.within/ss.total)
    
    return(eta)
}

# Compare a set of connectivity maps to each other
# The input is a correlation matrix where each column is the connectivity between one region/voxel with other regions/voxels (rows). This column or vector is referred to as a connectivity map.
# We will compare the similarity of the different connectivity maps using eta square
## Arguments
#### - cor.mat => matrix of correlations or some other similarity measure
## Return Value
#### - etas => matrix of eta-square values (i.e. similarity between each column with every other column or similarity of every connectivity map with every other connectivity map)
ni.eta = function(cor.mat) {
    V = nrow(cor.mat)   # number of voxels
    N = ncol(cor.mat)   # number of regions
    
    etas = matrix(1, N, N)
    
    for (xx in 1:(N-1)) {
        for (yy in (xx+1):N) {
            x = cor.mat[,xx]
            y = cor.mat[,yy]
            eta = etasq(x,y)
            etas[xx,yy] = eta
            etas[yy,xx] = eta
        }
    }
    
    return(etas)
}
